package com.wx.network.handler;

import com.wx.content.DataType;
import com.wx.network.codec.RpcProtocol;
import com.wx.network.handler.handlerInterface.RpcResponseOperate;
import com.wx.network.heartbeat.HeatBeatPackage;
import com.wx.network.mesg.Request;
import com.wx.network.mesg.Response;
import com.wx.util.IpUtil;
import com.wx.util.SerializationUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

/**
 * 处理服务器响应的心跳
 */
@Slf4j
public class PangHeartBeatHandler implements RpcResponseOperate {

    @Override
    public void handleData(ChannelHandlerContext channelHandlerContext, Response response) {
        HeatBeatPackage result = (HeatBeatPackage) response.getResult();
        log.info("客户端收到 服务器心跳：",result);
    }

    public void sendHeartBeat(ChannelHandlerContext channelHandlerContext){
        //要向服务器发送心跳包
        RpcProtocol protocol = new RpcProtocol();
        Request request = new Request();
        request.setDataType(DataType.PINFG);
        HeatBeatPackage pingHeartPackage = new HeatBeatPackage();
        pingHeartPackage.setIp(IpUtil.getLocalIp());
        request.setHeatBeatPackage(pingHeartPackage);
        //协议转换
        byte[] bytes = SerializationUtil.serialzationObj(request);
        protocol.setData(bytes);
        protocol.setLength(bytes.length);
        channelHandlerContext.writeAndFlush(protocol);
    }
}
